package com.gzxw.mogublog.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.WebNavbar;
import com.gzxw.mogublog.xo.mapper.WebNavbarMapper;
import com.gzxw.mogublog.xo.service.WebNavbarService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WebNavbarServiceImpl extends ServiceImpl<WebNavbarMapper, WebNavbar> implements WebNavbarService {

    @Override
    public List<WebNavbar> getAllList() {

        QueryWrapper<WebNavbar> oneNavbarQueryWrapper = new QueryWrapper<>();
        // 查询出一级菜单
        oneNavbarQueryWrapper.eq("status",1);
        oneNavbarQueryWrapper.orderByDesc("sort");
        oneNavbarQueryWrapper.eq("navbar_level",1);
        List<WebNavbar> oneNavBarList = this.list(oneNavbarQueryWrapper);
        List<String > oneNavBarUids = new ArrayList<>();

        oneNavBarList.forEach(item -> {
            oneNavBarUids.add(item.getUid());
        });

        // 查询二级菜单
        QueryWrapper<WebNavbar> twoNavbarQueryWrapper = new QueryWrapper<>();
        twoNavbarQueryWrapper.eq("status",1);
        twoNavbarQueryWrapper.in("parent_uid",oneNavBarUids);
        List<WebNavbar> twoNavBarList = this.list(twoNavbarQueryWrapper);

        // 给一级导航栏设置二级导航栏
        oneNavBarList.forEach(item -> {
            List<WebNavbar> childrenNavBar = new ArrayList<>();
            twoNavBarList.forEach(child -> {
                if (child.getParentUid().equals(item.getUid())) {
                    childrenNavBar.add(child);
                }
            });
            Collections.sort(childrenNavBar);
            item.setChildWebNavbar(childrenNavBar);
        });

        return oneNavBarList;
    }
}
